package com.itheima.dao.impl;

import com.itheima.dao.RoleDao;
import com.itheima.domain.Role;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

import javax.annotation.Resource;

/**
 * @author ZhangyiA
 * @describe:
 * @date 2021/06/24
 */

@Repository("roleDao")
public class RoleDaoImpl implements RoleDao {

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    public List<Role> findAll() {
        String sql = "select * from sys_role";
        List<Role> roleList = jdbcTemplate.query(sql , new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }

    public void save(Role role) {
        jdbcTemplate.update("insert into sys_role values(?,?,?)" , null , role.getRoleName() , role.getRoleDesc());
    }

    public List<Role> findRoleByUserId(long id) {
        String sql = "select * from sys_user_role ur , sys_role r where ur.roleId = r.id and ur.userId = ?";
        List<Role> roles = jdbcTemplate.query(sql ,  new BeanPropertyRowMapper<Role>(Role.class) , id);
        return roles;
    }
}
